package com.yhny.lykj.yuhannongye.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.longsichao.lscframe.ctrl.DataManager;
import com.longsichao.lscframe.model.LSCModel;
import com.yhny.lykj.yuhannongye.Adapter.AddressListAdapter;
import com.yhny.lykj.yuhannongye.R;
import com.yhny.lykj.yuhannongye.app.BaseActivity;
import com.yhny.lykj.yuhannongye.app.BaseRefreshListAdapter;
import com.yhny.lykj.yuhannongye.model.AddressListModel;
import com.yhny.lykj.yuhannongye.net.NetHelper;
import com.yhny.lykj.yuhannongye.tools.LCLog;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import java.util.ArrayList;

/**
 * 我的收货地址
 */
public class AddressListActivity extends BaseActivity implements BaseRefreshListAdapter.OnItemClickListener {
    int tag;
    public static AddressListActivity addressListActivity;
    private TextView rightTv;
    private TextView tipTv;
    private AddressListAdapter adapter;
    private ArrayList<AddressListModel.ListEntity> addressListModel;
    private RecyclerView recyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address_list);
        addressListActivity = this;
        tag = getIntent().getIntExtra(FROM_TYPE_ID,0);
        TextView topName = (TextView)findViewById(R.id.name_top_bar);
        recyclerView = (RecyclerView)findViewById(R.id.list);
        findViewById(R.id.left_group).setOnClickListener(this);
        findViewById(R.id.left_text).setOnClickListener(this);
        tipTv = (TextView)findViewById(R.id.tip);
        topName.setText("我的地址");
        findViewById(R.id.right_group).setOnClickListener(this);
        rightTv = (TextView)findViewById(R.id.right_text);
        rightTv.setOnClickListener(this);
        rightTv.setText("添加");
        rightTv.setVisibility(View.VISIBLE);
        addressListModel = new ArrayList<>();
        adapter = new AddressListAdapter(addressListModel,this,tag);
        adapter.setOnItemClickListener(this);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(adapter);
        recyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(this).sizeResId(R.dimen.item_vertical_margin).colorResId(R.color.colorDivider_line).build());
        showProgressDialog(R.string.text_loading);
        DataManager.getData(MODEL_PROCESS_0, NetHelper.User.getAddressList(), this);
    }



    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.left_text:
            case R.id.left_group:{
                finish();
                break;
            }
            case R.id.right_group:
            case R.id.right_text:{
                Intent intent = new Intent(this,AddAddressActivity.class);
                intent.putExtra(ADDRESS_VIEW,ADDRESS_NEW);
                startActivity(intent);
                break;
            }
        }
    }

    @Override
    protected void onResume() {
        DataManager.getData(MODEL_PROCESS_0, NetHelper.User.getAddressList(), this);
        super.onResume();
    }

    @Override
    public boolean onModel(int caseId, LSCModel model) {
        if (super.onModel(caseId, model)||model==null) {
            dismissProgressDialog();
            return false;
        }
        switch (caseId){
            case MODEL_PROCESS_0:{
                AddressListModel listmodel = (AddressListModel)model;
                if (isDataEmpty(listmodel)){
                    showNoDataMsg(true);
                }else{

                    if (listmodel.data!=null) {
                        addressListModel.clear();
                        addressListModel.addAll(listmodel.data);
                        LCLog.e("size = "+addressListModel.size());
                        adapter.notifyDataSetChanged();
                        showNoDataMsg(false);
                    }else{
                        showNoDataMsg(true);
                    }
                }
                dismissProgressDialog();
                break;
            }
        }
        dismissProgressDialog();
        return false;
    }
    private void showNoDataMsg(boolean isShow) {
        tipTv.setVisibility(isShow ? View.VISIBLE : View.GONE);
        recyclerView.setVisibility(isShow ? View.GONE : View.VISIBLE);
    }
    @Override
    public void onItemClick(int position) {
        if (tag==FROM_TYPE_VIEW_CREAT_ORDER){
            Intent intent = new Intent(this,OrderInfoActivity.class);
            intent.putExtra("address",addressListModel.get(position).adress);
            intent.putExtra("name",addressListModel.get(position).name);
            intent.putExtra("mobile",addressListModel.get(position).tel);
            setResult(tag, intent);
            finish();
        }

    }



}
